
pkgname=openssl
pkgver=1.0.2.15
_pkgver=1.0.2o
pkgrel=2
pkgdesc='The Open Source toolkit for Secure Sockets Layer and Transport Layer Security'
arch=('x86_64')
url='https://www.openssl.org'
license=('custom:BSD')
depends=('perl' 'zlib')
optdepends=('ca-certificates')
options=('!makeflags')
backup=('etc/ssl/openssl.cnf')
source=("https://www.openssl.org/source/${pkgname}-${_pkgver}.tar.gz"
        'no-rpath.patch'
        'ca-dir.patch'
        'bctest'
        'pod2mantest'
        "https://github.com/openssl/openssl/commit/349a41da1ad88ad87825414752a8ff5fdd6a6c3f.patch")
sha256sums=('ec3f5c9714ba0fd45cb4e087301eb1336c317e0d20b575a125050470e8089e4d'
            '754d6107a306311e15a1db6a1cc031b81691c8b9865e8809ac60ca6f184c957c'
            '9e8126f3a748f4c1d6fe34d4436de72b16a40e97a6d18234d2e88caa179d50c4'
            'fe30a5b9ca799beee04dcded04ba0c7eae32ee51ad9dd07d4cff8b6b0c949381'
            'cb429606a5b1df33c4133324f329948ccd4565bac497afc03e95fdf62ede1dca'
            '7c9cde92a3d72dbfdf741cdc0d1b6e0f866fce38357da943c79beee4d33967f7')

build() {
	cd ${pkgname}-${_pkgver}
	# add missing files to run bc test and install commands, 1.0.2e tar issue
	#cp $srcdir/bctest test
	#cp $srcdir/pod2mantest util
	
	openssltarget='linux-x86_64'
	optflags='enable-ec_nistp_64_gcc_128'

	patch -p0 -i ${srcdir}/no-rpath.patch
	# set ca dir to /etc/ssl by default
	patch -p0 -i ${srcdir}/ca-dir.patch
	# https://www.openssl.org/news/secadv/20180416.txt, will be included in openssl 1.0.2p
	patch -p1 -i ${srcdir}/349a41da1ad88ad87825414752a8ff5fdd6a6c3f.patch
	
	
	./Configure --prefix=/usr --openssldir=/etc/ssl --libdir=lib \
		shared zlib ${optflags} \
		${openssltarget} \
		-Wa,--noexecstack ${CFLAGS} ${LDFLAGS} \

	make depend
	make
}

check() {
	cd ${pkgname}-${_pkgver}
	# the test fails due to missing write permissions in /etc/ssl
	# revert this patch for make test
	patch -p0 -R -i ${srcdir}/ca-dir.patch
	make test
	patch -p0 -i ${srcdir}/ca-dir.patch
}

package() {
	cd ${pkgname}-${_pkgver}
	make INSTALL_PREFIX=${pkgdir} MANDIR=/usr/share/man MANSUFFIX=ssl install
	
	install -D -m644 LICENSE ${pkgdir}/usr/share/licenses/${pkgname}/LICENSE
}
